﻿using Prism.Regions;
using System.Windows.Controls;
using CoreModule.Controls;
using System.Windows.Data;
using System.Windows;
using System.Linq;
using System.Windows.Media;

namespace UIModule.Views
{
    /// <summary>
    /// Interaction logic for MeasurementResult
    /// </summary>
    public partial class MeasurementResult : UserControl
    {

        /// <summary>
        /// 
        /// </summary>
        CoreModule.DbColumnManager DbColumnManager { get; set; } = CoreModule.DbColumnManager.Load();

        public MeasurementResult(IRegionManager regionManager)
        {
      
            InitializeComponent();
            //var localRegionManager = regionManager.CreateRegionManager();
            //RegionManager.SetRegionManager(this, localRegionManager);
        }
        private void ViewPort_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
        
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            
            var fixCount = 9;

            var stackPanels= CoreModule.Extension.GetChildObjects<StackPanel>(this.clnContainer, typeof(StackPanel));

            var checkedList = DbColumnManager.GetCheckedList();

            for (int i = 0; i < checkedList.Count; i++)
            {
                var item = stackPanels[fixCount + i];
                item.Visibility= System.Windows.Visibility.Visible;

               var  textBlocks= CoreModule.Extension.GetChildObjects<TextBlock>(item, typeof(TextBlock),false);

                foreach (var textBlock in textBlocks)
                {
                    textBlock.Text= checkedList[i].ResultTitle;
                    textBlock.ToolTip = checkedList[i].FieldName;
                }
                var textBoxs = CoreModule.Extension.GetChildObjects<HandyControl.Controls.TextBox>(item, typeof(HandyControl.Controls.TextBox),false);
                foreach (var textBox in textBoxs)
                {
                    Binding bind = new Binding();
                    bind.Path = new PropertyPath(string.Format("MeasurementResultInfo.{0}", checkedList[i].ColumnName));
                    bind.Mode = BindingMode.TwoWay;
                    bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                    textBox.SetBinding(TextBox.TextProperty, bind);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {

           // this.svContainer.MinHeight = this.Height - 460;
            this.svContainer.Height = this.ActualHeight - 700;
        }

        private void txt_GotFocus(object sender, RoutedEventArgs e)
        {
            var textBox = sender as System.Windows.Controls.TextBox;

            textBox.BorderBrush = new SolidColorBrush(Color.FromRgb(0xEB, 0x1C, 0x2A));
           
            textBox.BorderThickness = new Thickness(1);
        }

        private void txt_LostFocus(object sender, RoutedEventArgs e)
        {
            var textBox = sender as System.Windows.Controls.TextBox;
            textBox.BorderBrush = new SolidColorBrush(Color.FromRgb(0xF1, 0xF0, 0xF5));
            textBox.BorderThickness = new Thickness(0);
        }

        //private void txtBMI_GotFocus(object sender, RoutedEventArgs e)
        //{
        //    var textBox = sender as System.Windows.Controls.TextBox;
        //    textBox.BorderBrush = new SolidColorBrush(Color.FromRgb(0xF1, 0xF0, 0xF5));

        //    textBox.BorderThickness = new Thickness(0);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void txtBMI_LostFocus(object sender, RoutedEventArgs e)
        //{
        //    var textBox = sender as System.Windows.Controls.TextBox;
        //    textBox.BorderBrush = new SolidColorBrush(Color.FromRgb(0xF1, 0xF0, 0xF5));
        //    textBox.BorderThickness = new Thickness(0);
        //}
    }
}
